package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.DestinationQuery;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.repository.RegionRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.DBHelp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class DestinationServiceImpl implements IDestinationService {
    

    @Autowired
    private DestinationRepository destinationRepository;
    
    @Autowired
    private IRegionService regionService;

    @Autowired
    private MongoTemplate template;

    @Autowired
    private RegionRepository regionRepository;

    @Override
    public void save(Destination destination) {
        destination.setId(null);
        destinationRepository.save(destination);
    }

    @Override
    public void update(Destination destination) {
        destinationRepository.save(destination);
    }

    @Override
    public void delete(String id) {
        destinationRepository.deleteById(id);
    }

    @Override
    public Destination get(String id) {
        return destinationRepository.findById(id).orElse(null);
    }

    @Override
    public List<Destination> list() {
        return destinationRepository.findAll();
    }

    @Override
    public List<Destination> queryDestByRegionId(String id) {

        Region region = regionService.get(id);

        List<String> refIds = region.getRefIds();

        return destinationRepository.findByIdIn(refIds);

    }

    @Override
    public void updateIshot(int hot, String id) {

        Query query = new Query();

        query.addCriteria(Criteria.where("_id").is(id));

        Update update = new Update();

        update.set("ishot", hot);

        template.updateMulti(query, update, Region.class);
    }

    @Override
    public Page<Destination> query(DestinationQuery qo) {

        Query query = new Query();

        if (StringUtils.hasLength(qo.getKeyword())){
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        }

        if (StringUtils.hasLength(qo.getParentId())){
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        }

        Pageable pageable = PageRequest.of(qo.getCurrentPage() -1, qo.getPageSize(), Sort.Direction.ASC, "_id");

        return DBHelp.query(template, pageable, query, Destination.class, qo);
    }

    @Override
    public List<Destination> queryToasts(String parentId) {

        List<Destination> list = new ArrayList<>();

        this.createToasts(list, parentId);

        //倒序
        Collections.reverse(list);

        return list;
    }
    
    private void createToasts(List<Destination> list, String parentId){

        if (!StringUtils.hasLength(parentId)){
            return;
        }

        Destination dest = this.get(parentId);
        list.add(dest);

        if (StringUtils.hasLength(dest.getParentId())){

            createToasts(list, dest.getParentId());
        }

    }

    @Override
    public List<Destination> queryHotDestination(String id) {

        List<Destination> list = new ArrayList<>();

        if ("-1".equals(id)){
            list = destinationRepository.findByParentName("中国");
        }else {
            list = this.queryDestByRegionId(id);
        }

        for (Destination dest : list) {

           Pageable pageable = PageRequest.of(0, 5, Sort.Direction.ASC,"_id");

           List<Destination> children =  destinationRepository.findByParentId(dest.getId(), pageable);

           dest.setChildren(children);
        }

        return list;
    }

}
